package com.cakeclient.clientsoap.contract;

import org.ksoap2.serialization.NullSoapObject;
import org.ksoap2.serialization.SoapObject;


public class Client extends ContractObject {
    public int id;
    public String name;
    public String firstname;
    public int isMale;
    public String deviceID;
    public String phoneNumber;
    public GPSLocation latestKnownPosition;
    public Address livingPlace;

    public Client() {

    }

    public Client(com.cakeclient.Model.Client client) {
        if (client.getRemoteID() != null)
            id = Integer.valueOf(client.getRemoteID().toString());
        name = client.getName();
        firstname = client.getFirstName();
        isMale = (client.getSex() == com.cakeclient.Model.Gender.MALE) ? 1 : 0;
        deviceID = client.getDeviceID();
        phoneNumber = client.getPhoneNumber();
        latestKnownPosition.latitude = client.getLatestKnownPosition().getLocation().getLatitude();
        latestKnownPosition.longitude = client.getLatestKnownPosition().getLocation().getLongitude();
        livingPlace = new Address(client.getLivingPlace().getAddress());
    }

    @Override
    public void fill(SoapObject soapObject) {
        if (!(soapObject.getPropertySafely("id") instanceof NullSoapObject))
            this.id = Integer.parseInt(soapObject.getPropertyAsString("id"));
        if (!(soapObject.getPropertySafely("name") instanceof NullSoapObject))
            this.name = soapObject.getPropertyAsString("name");
        if (!(soapObject.getPropertySafely("firstname") instanceof NullSoapObject))
            this.firstname = soapObject.getPropertyAsString("firstname");
        if (!(soapObject.getPropertySafely("isMale") instanceof NullSoapObject))
            this.isMale = Integer.parseInt(soapObject.getPropertyAsString("isMale"));
        if (!(soapObject.getPropertySafely("deviceID") instanceof NullSoapObject))
            this.deviceID = soapObject.getPropertyAsString("deviceID");
        if (!(soapObject.getPropertySafely("phoneNumber") instanceof NullSoapObject))
            this.phoneNumber = soapObject.getPropertyAsString("phoneNumber");

        this.latestKnownPosition = new GPSLocation();
        if (!(soapObject.getPropertySafely("latestKnownPosition") instanceof NullSoapObject)) {
            latestKnownPosition.fill((SoapObject) soapObject.getPropertySafely("latestKnownPosition"));
        }
        this.livingPlace = new Address();
        if (!(soapObject.getPropertySafely("livingPlace") instanceof NullSoapObject)) {
            livingPlace.fill((SoapObject) soapObject.getPropertySafely("livingPlace"));
        }
    }
}
